package cn.hgpu.servlet;

import cn.hgpu.mapper.AdminMapper;
import cn.hgpu.pojo.Admin;
import cn.hgpu.utils.MybatisUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends javax.servlet.http.HttpServlet {
    //获取dao层接口
    AdminMapper adminMapper = MybatisUtils.getMapper(AdminMapper.class);

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //1.接收表单参数
        String admin = req.getParameter("admin");
        String password = req.getParameter("password");

        //2.处理业务逻辑
            //调用方法
        Admin admin1 = adminMapper.selectByAdmin(admin);
        if(admin1==null){
            req.setAttribute("msg", "用户名不存在");
            //转发（跳转页面）
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
            return;
        }
        if(!admin1.getPassword().equals(password)){
            req.setAttribute("msg", "密码错误");
            //转发（跳转页面）
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
            return;
        }

        //3.响应处理结果
            //登录成功
            //重定向（跳转页面）
        req.getSession().setAttribute("admin",admin1);//设置属性标识
        resp.sendRedirect("/index.jsp");

    }
}
